package com.example.project22_editingmediaplayer.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.example.project22_editingmediaplayer.R;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class RowSongplayallsong extends ArrayAdapter<SongObject> {

	public ArrayList<SongObject> listData;
	
	public final ArrayList<Integer> listselected;
	private final Activity context;
	boolean checkAll_flag = false;
	boolean checkItem_flag = false;

	public RowSongplayallsong(ArrayList<SongObject> listData, Activity mContext) {
		super(mContext,R.layout.row_itemplayallsong,listData);
		this.listData = listData;
		this.context=mContext;
		listselected = new ArrayList<Integer>();
	}

	public void resetData(ArrayList<SongObject> listData) {
		this.listData = listData;
		this.notifyDataSetChanged();
	}

	public void addData(SongObject songObject) {
		listData.add(songObject);
		this.notifyDataSetChanged();
	}

	public ArrayList<SongObject> getAllData() {
		return this.listData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		try {
			if (!listData.isEmpty())
				return listData.size();
			else
				return 0;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;

	}

	@Override
	public SongObject getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int postition) {
		// TODO Auto-generated method stub
		return postition;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		RowHolder viewHolder = null;
		
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			convertView = inflator.inflate(R.layout.row_itemplayallsong, null);
			viewHolder = new RowHolder();
			viewHolder.imvAvarSong = (ImageView) convertView
					.findViewById(R.id.imvAvarSong);
			viewHolder.txvSong = (TextView) convertView.findViewById(R.id.txtNameSong);
			viewHolder.txvArtis = (TextView) convertView.findViewById(R.id.txtArtis);
			viewHolder.cb_play = (CheckBox) convertView.findViewById(R.id.cb_select);
			viewHolder.cb_play
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {

							int getPosition = (Integer) buttonView.getTag();
							listData.get(getPosition).setCheck(buttonView.isChecked());
						}

					});

			convertView.setTag(viewHolder);
			convertView.setTag(R.id.imvAvarSong,viewHolder.imvAvarSong);
			convertView.setTag(R.id.txtNameSong,viewHolder.txvSong);
			convertView.setTag(R.id.txtArtis,viewHolder.txvArtis);
			convertView.setTag(R.id.cb_select,viewHolder.cb_play);
		} else {

			viewHolder = (RowHolder) convertView.getTag();
		}
		viewHolder.cb_play.setTag(position);
		final SongObject songObject = listData.get(position);
		viewHolder = (RowHolder) convertView.getTag();
		viewHolder.txvSong.setText(songObject.get_name());
		viewHolder.txvArtis.setText(songObject.get_artist());
		Random rand = new Random();
		int currentSongIndex = rand
				.nextInt(SongResource.musicBackGround.length);
		viewHolder.imvAvarSong
				.setBackgroundResource(SongResource.musicBackGround[currentSongIndex]);
		viewHolder.imvAvarSong.setPadding(20, 20, 20, 20);
		
		viewHolder.cb_play.setChecked(songObject.isCheck());
		// set image view
		return convertView;
	}

	public static final class RowHolder {
		public ImageView imvAvarSong;
		public TextView txvSong;
		public TextView txvArtis;
		public CheckBox cb_play;
	}
}
